package com.hsmw.api.service;

import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwSmsMapper;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.util.SendSmsUtil;
import com.hsmw.domain.HsmwSms;
import com.htn.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


/**
 * @author WD
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwSmsService extends BaseService<HsmwSmsMapper, HsmwSms> {

	@Autowired
	private HsmwSmsMapper hsmwSmsMapper;

	@Autowired
	private CurrentHsmwUserUtils currentHsmwUserUtils;

	@Value(value = "${sms.appKey}")
	private String appKey;
	@Value(value = "${sms.channel}")
	private String channel;
	@Value(value = "${sms.smsSign}")
	private String smsSign;
	@Value(value = "${sms.appId}")
	private int appId;
	@Value(value = "${sms.loginSmstemplateId}")
	private int loginSmstemplateId;


	public String sendSmsCode(String username,String type) {
		String code = SendSmsUtil.sendSmsCode(appId, appKey, loginSmstemplateId, smsSign, username);
		//LoginUser user = currentHsmwUserUtils.getCurrentUserInfo();
		HsmwSms hsmwSms = new HsmwSms();
		hsmwSms.setCode(code);
		hsmwSms.setUsername(username);
		hsmwSms.setChannel(channel);
		hsmwSms.setLockVersion(0);
		if(StringUtils.isNotBlank(type)){
			hsmwSms.setType(type);
		}
		hsmwSms.setExpireTime(LocalDateTime.now().plusMinutes(15));
		hsmwSmsMapper.insert(hsmwSms);
		return code;
	}


}
